/*
 * Copyright (C) 2013, Pavel Samko <bulldozerbsg@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the "Pavel Samko" nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */

#include "vfsrealfs.h"
#include "vfscore.h"
#include "vfsutils.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int vfs_realfs_fclose(void * mod_data)
{
	return fclose((FILE *)mod_data);
}

int vfs_realfs_fflush(void * mod_data)
{
	return fflush((FILE *)mod_data);
}

int vfs_realfs_cheakmode_for_read(const char * mode)
{
	return mode && (!strcmp(mode, "rb") || !strcmp(mode, "br") || !strcmp(mode, "r"));
}

char * vfs_realfs_getpath(MOD * mod)
{
	size_t i;
	int has_path = 0;
	if (mod && mod->m_params && mod->m_params_count)
	{
		for (i = 0; i < mod->m_params_count; ++i)
		{
			if ((mod->m_params[i].m_key == VFS_PARAM_PATH_HAS) && (mod->m_params[i].m_data_size == sizeof(int)))
			{
				has_path = *(int *)mod->m_params[i].m_data;
				break;
			}
		}
		if (has_path)
		{
			for (i = 0; i < mod->m_params_count; ++i)
			{
				if (mod->m_params[i].m_key == VFS_PARAM_PATH)
				{
					return (char *)mod->m_params[i].m_data;
				}
			}
		}
	}
	return NULL;
}

void * vfs_realfs_fopen(const char * name, const char * mode, MOD * mod)
{
	char * name_dub;
	char * path;
	FILE * result = NULL;
	if (name && mode && mod && (mod->f_fwrite || vfs_realfs_cheakmode_for_read(mode)) && (path = vfs_realfs_getpath(mod)))
	{
		name_dub = (char *)malloc(strlen(path) + strlen(name) + 1);
		if (name_dub)
		{
			strcpy(name_dub, path);
			strcat(name_dub, name);
#ifdef _WIN32
			vfs_strstobs(name_dub);
#endif
			result = fopen(name_dub, mode);
			free(name_dub);
		}
	}
	return (void *)result;
}

size_t vfs_realfs_fread(void * dst, size_t size, size_t num, void * mod_data)
{
	return fread(dst, size, num, (FILE *)mod_data);
}

int vfs_realfs_fseek(void * mod_data, long int offset, int origin)
{
	return fseek((FILE *)mod_data, offset, origin);
}

long int vfs_realfs_ftell(void * mod_data)
{
	return ftell((FILE *)mod_data);
}

size_t vfs_realfs_fwrite(const void * src, size_t size, size_t num, void * mod_data)
{
	return fwrite(src, size, num, (FILE *)mod_data);
}
